package com.rrqs.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.litesuits.http.request.content.multi.MultipartBody;
import com.litesuits.http.request.content.multi.StringPart;
import com.rrqs.activity.common.Const;
import com.rrqs.activity.data.OrderOverNewBean;
import com.rrqs.net.AsyncHttpClient;
import com.rrqs.net.HttpResponseListener;
import com.rrqs.util.MyApplication;
import com.rrqs.utils.DateUtils;
import com.rrqs.utils.JsonUtils;
import com.rrqs.views.PullDownView;
import com.zadtek.xmqishi.R;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;

import static com.zadtek.xmqishi.R.id.status;

/**
 * Created by Thinkpad on 2019/5/5.
 */

public class OrderOverNewActivity extends BaseActivity {
    private ImageView backid;
    private TextView topview;
    private PullDownView pulldownview;
    private ListView listView;
    private ArrayList<OrderOverNewBean.ListEntity> orderoverlist;
    private ListAdapter listAdapter;
    private ImageView iv_rili;
    private int page = 1;
    private Date date = new Date();
    private RadioGroup rg;
    private int type = 1;//1已完成2已取消
    private TextView time,num;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.order_over_2);
        initView();
        initData();
    }
    private void initView(){
        orderoverlist = new ArrayList<>();
        pulldownview = (PullDownView) findViewById(R.id.pulldownview);
        listView = pulldownview.getListView();
        iv_rili = (ImageView) findViewById(R.id.iv_rili);
        iv_rili.setVisibility(View.VISIBLE);
        iv_rili.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(new Intent(v.getContext(), DatePickerActivity.class), 1000);
            }
        });
        listAdapter = new ListAdapter();
        listView.setAdapter(listAdapter);
        backid = (ImageView) findViewById(R.id.backid);
        topview = (TextView) findViewById(R.id.topview);
        topview.setText("已完成的任务");
        pulldownview.enableAutoFetchMore(true, 0);
        backid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                finish();
            }
        });
        pulldownview.setOnPullDownListener(new PullDownView.OnPullDownListener() {
            @Override
            public void onRefresh() {
                orderoverlist.clear();
                page = 1;
                initData();
            }

            @Override
            public void onLoadMore() {
                page++;
                initData();
            }
        });
        rg = findViewById(R.id.radiogroup);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId==R.id.ok){
                    type = 1;
                }else{
                    type = 2;
                }
                orderoverlist.clear();
                page = 1;
                initData();
            }
        });
        time = findViewById(R.id.time);
        num = findViewById(R.id.num);

    }
    private void initData(){
        AsyncHttpClient httpClient = new AsyncHttpClient(MyApplication.getInstace());
        SharedPreferences sp = com.rrqs.util.MyApplication.getInstace().getSharedPreferences(Const.PREF_NAME, Context.MODE_PRIVATE);
        String uid = sp.getString("uid", "");
        MultipartBody body = new MultipartBody();
        body.addPart(new StringPart("emp_id", uid));
        body.addPart(new StringPart("ince", "get_overorder_list"));
        body.addPart(new StringPart("page", page+""));
        body.addPart(new StringPart("status", "1"));
        body.addPart(new StringPart("type", ""+type));
        body.addPart(new StringPart("time", DateUtils.convertDateToStr(date,DateUtils.YYYY_MM_DD)));
        httpClient.post("http://xmpc.zadtek.com/mobile/Qs/index", body, new HttpResponseListener() {
            @Override
            public void onFailure(Throwable error) {
                super.onFailure(error);
            }
            @SuppressWarnings("unchecked")
            @Override
            public void onSuccess(String status, String result) {
                super.onSuccess(status, result);
                try {
                    Log.i("CYX","已完成订单"+result);
                    JSONObject jsonObject = new JSONObject(result);
                    Log.i("CYX","已完成订单"+jsonObject);
                }catch (Exception e){
                }
                OrderOverNewBean resp = JsonUtils.json2Class(result,OrderOverNewBean.class);
                if (resp.getStatus() == 1){
                    time.setText(resp.getDaytime());
                    if(type == 1) {
                        num.setText("已完成" + resp.getTotal() + "单");
                    }else {
                        num.setText("已取消" + resp.getTotal() + "单");
                    }

                    for(OrderOverNewBean.ListEntity entity:resp.getList()){
                        orderoverlist.add(entity);
                    }
                    pulldownview.notifyDidRefresh(true);
                    listAdapter.notifyDataSetChanged();
                    if (resp.getList().size()>0) {
                        pulldownview.notifyDidLoadMore(false);
                    } else {
                        pulldownview.notifyDidLoadMore(true);
                    }
                    RadioButton btn_ok = findViewById(R.id.ok);
                    btn_ok.setText("已完成(" + resp.getTotalyes() + ")");
                    RadioButton btn_cancel = findViewById(R.id.cancel);
                    btn_cancel.setText("已取消(" + resp.getTotalno() + ")");

                }
            }
        });
    }
    public class ListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return orderoverlist != null ? orderoverlist.size() : 0;
        }

        @Override
        public Object getItem(int position) {
            return orderoverlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (orderoverlist != null) {
                ViewHolder holder = null;
                if (convertView == null) {
                    convertView = LayoutInflater.from(OrderOverNewActivity.this).inflate(
                            R.layout.order_over_dayitem, null);
                    holder = new ViewHolder();

                    holder.mainord = (TextView) convertView
                            .findViewById(R.id.mainord);
                    holder.psmoney = (TextView) convertView
                            .findViewById(R.id.psmoney);
                    holder.detailname = (TextView) convertView
                            .findViewById(R.id.detailname);
                    holder.address = (TextView) convertView
                            .findViewById(R.id.address);
                    holder.shangjialiushuihao = (TextView) convertView
                            .findViewById(R.id.shangjialiushuihao);
                    holder.status = (TextView) convertView
                            .findViewById(status);
                    holder.songdashijian = (TextView) convertView
                            .findViewById(R.id.songdashijian);
                    holder.yongshi = (TextView) convertView
                            .findViewById(R.id.yongshi);
                    holder.type = (TextView) convertView
                            .findViewById(R.id.type);
                    convertView.setTag(holder);

                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                if (convertView != null) {
                    if (orderoverlist.size() > position) {
                        holder.mainord
                                .setText("总订单号："+orderoverlist.get(position).getMain_id());
                        holder.psmoney.setText("交易金额:¥"+orderoverlist.get(position).getShipping_fee()+"元");
                        holder.detailname.setText(orderoverlist.get(position).getGet_road());
                        holder.address.setText(orderoverlist.get(position).getAddress());
                        holder.shangjialiushuihao.setText("商家流水号："+orderoverlist.get(position).getLiushui());
                        holder.type.setText(orderoverlist.get(position).getDantype());
                        holder.status.setText("已送达");
                        holder.songdashijian.setText(orderoverlist.get(position).getSend_time());
                        holder.yongshi.setText("用时："+orderoverlist.get(position).getXd_time());

                        convertView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View arg0) {
                                // TODO Auto-generated method stub
                                Intent intent = new Intent(OrderOverNewActivity.this, OrderDetailNewActivity.class);
                                intent.putExtra("mainid", orderoverlist.get(position)
                                        .getMain_id());
                                OrderOverNewActivity.this.startActivity(intent);
                            }
                        });
                    }
                }
            }
            return convertView;
        }

    }

    static class ViewHolder {
        TextView mainord;
        TextView psmoney;
        TextView detailname;	//取货店铺名称
        TextView address;	//送货地址
        TextView shangjialiushuihao,status,songdashijian,yongshi,type;


    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            assert data != null;
            if (requestCode == 1000) {
                Date mStartDate = new Date(data.getLongExtra("date", 0));
                date = mStartDate;
                page = 1;
                orderoverlist.clear();
                initData();
//				tv_start_date.setText(DateUtils.convertDateToStr(mStartDate));
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
